package com.simplemonkey.widgets;
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details:
 * http://www.gnu.org/copyleft/lesser.html#TOC3
 */
import java.awt.Color;
import java.awt.Font;
import java.nio.FloatBuffer;

import com.jme.image.Texture;
import com.jme.util.geom.BufferUtils;
import com.simplemonkey.IResizeListener;
import com.simplemonkey.util.Dimension;
import com.simplemonkey.util.TextTextureBuilder;

public class TextWidget extends TextureWidget {
	private String text;
	private Texture textTexture;
	private int textureApplyMode = Texture.AM_DECAL;
	public int getTextTextureApplyMode() {
		return textureApplyMode;
	}

	public void setTextTextureApplyMode(int textureApplyMode) {
		this.textureApplyMode = textureApplyMode;
		if(this.textTexture != null)
			this.textTexture.setApply(textureApplyMode);
	}

	private Color textColor = Color.black;
	private Font textFont = Font.decode("Sans-Serif").deriveFont(12).deriveFont(Font.PLAIN);
	
	public Color getTextColor() {
		return textColor;
	}

	public void setTextColor(Color textColor) {
		this.textColor = textColor;
		this.setText(this.getText());
	}
	
	public void setFont(Font font)
	{
		this.textFont = font;
	}

	/**
	 * The texture unit that text should be applied to. 
	 */
	private int textUnit = 1;
	
	/**
	 * The texture unit that text should be applied to. 
	 */
	public int getTextUnit() {
		return textUnit;
	}

	/**
	 * The texture unit that text should be applied to. 
	 */
	public void setTextUnit(int textUnit) {
		this.textUnit = textUnit;
	}

	public TextWidget() {
		this("");
	}
	
	public TextWidget(String text) {
		super(text);
		super.addResizeListener(new IResizeListener()
		{

			
			public void resize(com.simplemonkey.util.Dimension newSize) {
				resizeTexture(new Dimension(getSpacingAppearance().getContentWidth()+ getSpacingAppearance().getPadding().getLeftPlusRight(),getSpacingAppearance().getContentHeight()+ getSpacingAppearance().getPadding().getBottomPlusTop()));
			}
			
		});
		this.setText(text);
	}
	private void resizeTexture(com.simplemonkey.util.Dimension newSize)
	{
		if(textTexture == null)
			return;
		FloatBuffer posBuffer = BufferUtils.createVector2Buffer(4);
		int width =textTexture.getImage().getWidth();
		int height =textTexture.getImage().getHeight();
		float relativeWidth =((float)width)/ ((float) newSize.getWidth());
		float relativeHeight = ((float)height)/((float) newSize.getHeight());
		relativeWidth = 1f/relativeWidth;
		relativeHeight = 1f/relativeHeight;
		posBuffer.put(0.5f- relativeWidth/2f ).put(0.5f+ relativeHeight/2f );
		posBuffer.put(0.5f- relativeWidth/2f ).put(0.5f- relativeHeight/2f );
		posBuffer.put(0.5f+ relativeWidth/2f ).put(0.5f- relativeHeight/2f );
		posBuffer.put(0.5f+ relativeWidth/2f ).put(0.5f+ relativeHeight/2f );
		
		background.setTextureBuffer(0, posBuffer, this.getTextUnit());
	}
	public Texture getTextTexture() {
		return textTexture;
	}
	public String getText() {
		return text;
	}
	
	public void setText(String text)
	{
		setText(text,this.getTextColor());
	}
			
	public void setText( String text, Color color) {
		this.text = text;
		this.textColor = color;
		if(text == "")
		{
			textTexture = null;
			this.setSizeToMinSize();
			super.removeTexture(1);
		}else
		{
			java.awt.Dimension size = new java.awt.Dimension();
			textTexture = TextTextureBuilder.buildTextTexture( text,textFont,color,size);
			textTexture.setWrap(Texture.WM_BCLAMP_S_BCLAMP_T);
			this.setMinContentSize(new Dimension(size.width, size.height));
			
		
			this.updateMinSize();
			this.setSizeToMinSize();
			
			textTexture.setApply(this.getTextTextureApplyMode());
			super.setTexture(textTexture,textUnit);
		//	this.resizeTexture(this.getSize());
		}
	}
	
	
}
